package ink.xiaobaibai.service;

import ink.xiaobaibai.serviceOxy.monthly.impl.DelayMonControlServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;
import java.util.Set;

/**
 * @description: 关机保护服务
 * @author: 小白白
 * @create: 2021-06-11
 **/

@Service
public class ShutdownProtectionService implements ApplicationRunner {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DelayMonControlServiceImpl delayMonDeviceControlService;

    private static final String activeSafe = "ad-activeSafe";

    /**
     * 管理端:
     * 1.激活服务
     * 2.定点:管理控制的机器开启
     */

    @PreDestroy
    public void destroy() throws Exception {
        /**
         * 激活服务保护
         */
        this.redisTemplate.opsForSet().add(activeSafe, this.delayMonDeviceControlService.delayQueue.set);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        /**
         * 时间统计呢? 内部采用date计时,所以不用怕停止时间,造成时间错误
         */
        /**
         * 激活服务还原
         */
        Set set1 = this.redisTemplate.opsForSet().members(activeSafe);
        this.redisTemplate.delete(activeSafe);
        this.delayMonDeviceControlService.delayQueue.set.addAll(set1);
    }

}
